package u1project.server;

import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ServerThread implements Runnable {
    private final Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    //login
    public Response login(Request req) {
        Response resp = null;
        String uname = (String) req.get("uname");
        String upass = (String) req.get("upass");
        if ("laoliu".equals(uname) && "dashuaige".equals(upass)) {
            resp = new Response(true, "登录成功");
        } else {
            resp = new Response(false, "错误的用户名或者密码");
        }
        return resp;
    }

    public Response zhuce(Request req) {
        Response resp = null;
        //两次密码一样，密码长度必须大于等于6个
        String uname = (String) req.get("uname");
        String upass = (String) req.get("upass");
        String pass2 = (String) req.get("repeatpass");
        if (upass.equals(pass2) == false) {
            resp = new Response(false, "两次密码不一致");
        } else if (upass.length() < 6) {
            resp = new Response(false, "密码太短");
        } else {
            resp = new Response(true);
        }
        return resp;
    }

    //文件上传
    //发一个文件名，发一个byte数组（文件内容）fileName,content
    public Response upload(Request req) {
        try {
            String fname = (String) req.get("fileName");
            byte[] b = (byte[]) req.get("content");
            //保存到文件里去。。。目录我来指定
            FileOutputStream fout = new FileOutputStream("E:\\temp\\" + fname);
            fout.write(b);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(false, "上传出现异常：" + e);
        }
        return new Response(true);
    }

    @Override
    public void run() {
        //所有的传输都是Request和Response作为载体
        ObjectInputStream oin = null;
        ObjectOutputStream oout = null;
        System.out.println(s + "链接了。。。");
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            oin = new ObjectInputStream(in);
            oout = new ObjectOutputStream(out);
            //每一次循环，从客户端读一个Request,返回一个Response
            while (true) {
                Request req = (Request) oin.readObject();
                //处理
                Response resp = null;//
                //3:去掉if else
                Method method = this.getClass().getDeclaredMethod(req.getAction(), Request.class);
                resp = (Response)method.invoke(this, req);

                //2:
//                if (req.getAction().equals("login")) {
//                    resp = login(req);
//                } else if (req.getAction().equals("zhuce")) {
//                    resp = zhuce(req);
//                } else if (req.getAction().equals("upload")) {
//                    resp = upload(req);
//                } else {
//                    resp = new Response(false, "次操作" + req.getAction() + "还不支持");
//                }
              
                oout.writeObject(resp);
                oout.reset();//防止对象发上次的！！！！
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println(s + "退出了。。。");
                s.close();
            } catch (Exception e) {

            }
        }
    }
}
